﻿using Framework.Common;

namespace Bright.NetCore.Api.Helpers
{
    /// <summary>
    /// Session操作帮助
    /// </summary>
    public static class SessionHelper
    {
        private static IHttpContextAccessor _httpContextAccessor;

        public static void Configure(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        private static ISession Session => _httpContextAccessor.HttpContext.Session;


        /// <summary>
        /// 设置 Session 值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void Set(string key, string value) => Session.SetString(key, value);

        /// <summary>
        /// 获取 Session 值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>值</returns>
        public static string Get(string key) => Session.GetString(key);

        /// <summary>
        /// 获取 Session 值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get<T>(string key)
        {
            var sessionStr = Get(key);
            return sessionStr.ConvertToObject<T>();
        }

        /// <summary>
        /// 移除 Session 值
        /// </summary>
        /// <param name="key">键</param>
        public static void Remove(string key)
        {
            Session.Remove(key);
        }

        /// <summary>
        /// 清空所有 Session 值
        /// </summary>
        public static void Clear()
        {
            Session.Clear();
        }
    }
}
